package org.jeecg.basic.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.basic.entity.MesBomHeaderSetup;
import org.jeecg.basic.entity.MesBomItemSetup;
import org.jeecg.basic.service.IMesBomHeaderSetupService;
import org.jeecg.basic.service.IMesBomItemSetupService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 产品主数据
 * @Author: LiCY
 * @Date: 2023-06-16
 * @Version: V1.0
 */
@Api(tags = "产品主数据")
@RestController
@RequestMapping("/basic/mesBomHeaderSetup")
@Slf4j
public class MesBomHeaderSetupController extends JeecgController<MesBomHeaderSetup, IMesBomHeaderSetupService> {

    @Autowired
    private IMesBomHeaderSetupService mesBomHeaderSetupService;

    @Autowired
    private IMesBomItemSetupService mesBomItemSetupService;



    /*---------------------------------主表处理-begin-------------------------------------*/

    /**
     * 分页列表查询1
     *
     * @param mesBomHeaderSetup
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "产品主数据-分页列表查询", notes = "产品主数据-分页列表查询")
    @GetMapping(value = "/list")
    @DS("#header.his")
    public Result<IPage<MesBomHeaderSetup>> queryPageList(MesBomHeaderSetup mesBomHeaderSetup,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {
        QueryWrapper<MesBomHeaderSetup> queryWrapper = QueryGenerator.initQueryWrapper(mesBomHeaderSetup, req.getParameterMap());
        Page<MesBomHeaderSetup> page = new Page<MesBomHeaderSetup>(pageNo, pageSize);
        IPage<MesBomHeaderSetup> pageList = mesBomHeaderSetupService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param mesBomHeaderSetup
     * @return
     */
    @AutoLog(value = "产品主数据-添加")
    @ApiOperation(value = "产品主数据-添加", notes = "产品主数据-添加")
    @PostMapping(value = "/add")
    @DS("#header.line")
    public Result<String> add(@RequestBody MesBomHeaderSetup mesBomHeaderSetup) {
        if (StringUtils.isBlank(mesBomHeaderSetup.getPnGroup())) {
            mesBomHeaderSetup.setPnGroup(mesBomHeaderSetup.getDescLocal());
        }
        mesBomHeaderSetupService.save(mesBomHeaderSetup);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mesBomHeaderSetup
     * @return
     */
    @AutoLog(value = "产品主数据-编辑")
    @ApiOperation(value = "alarmDuration", notes = "产品主数据-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("#header.line")
    public Result<String> edit(@RequestBody MesBomHeaderSetup mesBomHeaderSetup) {
        mesBomHeaderSetupService.updateById(mesBomHeaderSetup);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param mesBomHeaderSetup
     * @return
     */
    @AutoLog(value = "产品主数据-通过id删除")
    @ApiOperation(value = "产品主数据-通过id删除", notes = "产品主数据-通过id删除")
    @RequestMapping(value = "/delete")
    @DS("#header.line")
    public Result<String> delete(@RequestBody MesBomHeaderSetup mesBomHeaderSetup) {
        mesBomHeaderSetupService.delMain(mesBomHeaderSetup.getPartno());
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @AutoLog(value = "产品主数据-批量删除")
    @ApiOperation(value = "产品主数据-批量删除", notes = "产品主数据-批量删除")
    //@RequiresPermissions("org.jeecg.modules.demo:mes_bom_header_setup:deleteBatch")
    @RequestMapping(value = "/deleteBatch")
    @DS("#header.line")
    public Result<String> deleteBatch(@RequestBody List<MesBomHeaderSetup> list) {
        this.mesBomHeaderSetupService.delBatchMain(list);
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出
     *
     * @return
     */
    //@RequiresPermissions("org.jeecg.modules.demo:mes_bom_header_setup:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, MesBomHeaderSetup mesBomHeaderSetup) {
        return super.exportXls(request, mesBomHeaderSetup, MesBomHeaderSetup.class, "产品主数据");
    }

    /**
     * 导入
     *
     * @return
     */
    //@RequiresPermissions("org.jeecg.modules.demo:mes_bom_header_setup:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @DS("#header.line")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MesBomHeaderSetup.class);
    }
    /*---------------------------------主表处理-end-------------------------------------*/


    /*--------------------------------子表处理-产品BOM-begin----------------------------------------------*/

    /**
     * 通过主表ID查询
     *
     * @return
     */
    //@AutoLog(value = "产品BOM-通过主表ID查询")
    @ApiOperation(value = "产品BOM-通过主表ID查询", notes = "产品BOM-通过主表ID查询")
    @GetMapping(value = "/listMesBomItemSetupByMainId")
    @DS("#header.his")
    public Result<IPage<MesBomItemSetup>> listMesBomItemSetupByMainId(MesBomItemSetup mesBomItemSetup,
                                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                      HttpServletRequest req) {
        QueryWrapper<MesBomItemSetup> queryWrapper = QueryGenerator.initQueryWrapper(mesBomItemSetup, req.getParameterMap());
        queryWrapper.orderByAsc("OP");
        Page<MesBomItemSetup> page = new Page<MesBomItemSetup>(pageNo, pageSize);
        IPage<MesBomItemSetup> pageList = mesBomItemSetupService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param mesBomItemSetup
     * @return
     */
    @AutoLog(value = "产品BOM-添加")
    @ApiOperation(value = "产品BOM-添加", notes = "产品BOM-添加")
    @PostMapping(value = "/addMesBomItemSetup")
    @DS("#header.line")
    public Result<String> addMesBomItemSetup(@RequestBody MesBomItemSetup mesBomItemSetup) {
        mesBomItemSetupService.save(mesBomItemSetup);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mesBomItemSetup
     * @return
     */
    @AutoLog(value = "产品BOM-编辑")
    @ApiOperation(value = "产品BOM-编辑", notes = "产品BOM-编辑")
    @RequestMapping(value = "/editMesBomItemSetup", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("#header.line")
    public Result<String> editMesBomItemSetup(@RequestBody MesBomItemSetup mesBomItemSetup) {
//        mesBomItemSetupService.updateByMultiId(mesBomItemSetup);
        LambdaQueryWrapper<MesBomItemSetup> eq = new LambdaQueryWrapper<MesBomItemSetup>()
                .eq(MesBomItemSetup::getPartno, mesBomItemSetup.getPartno())
                .eq(MesBomItemSetup::getOp, mesBomItemSetup.getOp());
        List<MesBomItemSetup> list = mesBomItemSetupService.list(eq);

            LambdaUpdateWrapper<MesBomItemSetup> updateWrapper = new LambdaUpdateWrapper<MesBomItemSetup>();
            updateWrapper.eq(MesBomItemSetup::getPartno,mesBomItemSetup.getPartno())
                    .eq(MesBomItemSetup::getOp,mesBomItemSetup.getOp())
                    .eq(MesBomItemSetup::getItempartno,mesBomItemSetup.getItempartno());
            boolean isupdate = mesBomItemSetupService.update(mesBomItemSetup,updateWrapper);
            return isupdate ? Result.ok("修改成功!") : Result.error("修改失败!");

//        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "产品BOM-通过id删除")
    @ApiOperation(value = "产品BOM-通过id删除", notes = "产品BOM-通过id删除")
    @RequestMapping(value = "/deleteMesBomItemSetup")
    @DS("#header.line")
    public Result<String> deleteMesBomItemSetup(@RequestBody MesBomItemSetup mesBomItemSetup) {
        mesBomItemSetupService.deleteByMultiId(mesBomItemSetup);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "产品BOM-批量删除")
    @ApiOperation(value = "产品BOM-批量删除", notes = "产品BOM-批量删除")
    @RequestMapping(value = "/deleteBatchMesBomItemSetup")
    @DS("#header.line")
    public Result<String> deleteBatchMesBomItemSetup(@RequestBody List<MesBomItemSetup> list) {
//        this.mesBomItemSetupService.removeByIds(Arrays.asList(ids.split(",")));
        list.forEach(item->{
            mesBomItemSetupService.deleteByMultiId(item);
        });
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param param
     */
    @RequestMapping(value = "/importMould")
    @DS("#header.his")
    public ModelAndView importMould(HttpServletRequest request, HttpServletResponse response, @RequestParam  Map<String, Object> param) {
        mesBomItemSetupService.getImportMould(response);
        return null;
    }

    /**
     * 导出
     *
     * @return
     */
    @GetMapping (value = "/exportImportMould")
    @DS("#header.his")
    public void exportXsl(HttpServletRequest request, HttpServletResponse response, @RequestParam  Map<String, Object> param) {
        mesBomItemSetupService.exportImportMould(request,response,param);
    }

    /**
     * 导入
     *
     * @return
     */
    @RequestMapping(value = "/importMesBomItemSetup")
    @DS("#header.line")
    public Result<?> importMesBomItemSetup(HttpServletRequest request, HttpServletResponse response) {
        return mesBomItemSetupService.importMesBomItemSetup(request);
    }

    /*--------------------------------子表处理-产品BOM-end----------------------------------------------*/


}
